// Copyright 2007 the original author or authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package lichen.pages.admin.forum;

import lichen.entities.forum.Forum;
import lichen.entities.forum.ForumImpl;
import lichen.pages.Operations;
import lichen.pages.base.BasePage;
import lichen.services.forum.ForumClassService;

import org.apache.tapestry.annotations.Inject;
import org.apache.tapestry.annotations.InjectPage;

/**
 * 对论坛类别数据进行处理时候的页面类.
 * 
 * @author xiafei
 * @author <a href="mailto:jun.tsai@gmail.com">Jun Tsai</a>
 * @version $Revision: 24 $
 * @since 0.0.1
 */
public class FormForum extends BasePage {
	// 当前的操作.
	private String action;

	// 当前操作的论坛类别.
	private Forum forum;



	// 父论坛
	private Forum parentForum;

	// 浏览论坛的列表页面
	@InjectPage
	private ViewForum forumPage;

	@Inject
	private ForumClassService forumClassService;

	
	/** 固化的论坛数据 **/
	private Forum forumPassivated;

	// 处理新增时候的处理
	void onActivate() {
		this.forum = new ForumImpl();
	}

	void onActivate(String action, Forum forum) {
		this.action = action;
		
		if (Operations.EDIT.equals(action)) {
			this.forum = forum;
		} else if (Operations.ADD_CHILD.equals(action)) {
			this.parentForum = forum;
		}
		forumPassivated=forum;
	}

	Object[] onPassivate() {
		return new Object[] { action, this.forumPassivated};
	}

	public ViewForum onActionFromForumForm() {

		if (Operations.EDIT.equals(action)) {
			this.getHibernateTemplate().saveOrUpdate(this.forum);
		} else {
			forumClassService.saveTreeChildNode(this.forum, this.parentForum,
					ForumImpl.class);

		}

		return this.forumPage;
	}

	/**
	 * @return Returns the forum.
	 */
	public Forum getForum() {
		return forum;
	}

}
